package com.byd.modules.biz.controller;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.byd.common.utils.R;
import com.byd.modules.biz.entity.SuperviseEntity;
import com.byd.modules.biz.service.DashboardService;
 
@RestController
@RequestMapping("/dashboard")
public class DashboardController {
    Logger logger = Logger.getLogger(DashboardController.class);
    @Autowired
    DashboardService dashboardService;

    
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(String startTime,String endTime,String car1,String car2){
        Map<String,Object> res = dashboardService.queryList(startTime,endTime,car1,car2);
        return R.ok().put("page", res);
    }
    
    
    public static void main(String[] args) throws UnknownHostException {
        String index = "byd";
        Settings settings = Settings.builder().put("cluster.name", "biyadi-test").build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("101.89.68.208"), 9300));

       // MatchPhraseQueryBuilder mpq1 = QueryBuilders.matchPhraseQuery("car_series", "");
        // WildcardQueryBuilder mpq3 = QueryBuilders.wildcardQuery("brand.keyword", "*");
        RangeQueryBuilder mpq2 = QueryBuilders.rangeQuery("create_date").format("yyyy-MM-dd").from("2017-07-01").to("2018-01-01");
        //RangeQueryBuilder mpq2 = QueryBuilders.rangeQuery("post_time.keyword").format("yyyy-MM-dd").from("2017-07-01").to("2018-01-01");
        QueryBuilder qb = QueryBuilders.boolQuery().must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb);
        SearchResponse scrollResp = client.prepareSearch(index).setQuery(qb).get();
        long total = scrollResp.getHits().totalHits;
        qb = QueryBuilders.boolQuery().must(mpq2).must(QueryBuilders.existsQuery("result"));
        SearchResponse scrollResp1 = client.prepareSearch(index).setQuery(qb).get();
        long total1 = scrollResp1.getHits().totalHits;
        long total2 = total-total1;
        
        System.out.println(total+"-"+total1+"-"+total2);
        
    }
    
    
}